package com.jx.gocomtask.webservice.task;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.jx.gocomtask.webservice.DTO.MessageReturn;
import com.jx.gocomtask.webservice.DTO.StatusMessage;
import com.jx.gocomtask.webservice.entity.base.CountTodo;
import com.jx.gocomtask.webservice.repository.ExtTaskTodoRepository;
import com.jx.gocomtask.webservice.service.SendMessageService;
import com.jx.gocomtask.webservice.service.SystemSuperApp;

@Component("todoSend")  
public class TodoSendMessage {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TodoSendMessage.class);
	private final String method_single="message/single/send/status?";
	  @Autowired
	    RestTemplate restTemplate;
	  @Resource(name="apiurl")
		String gocomeApiUrl;
	  @Autowired
		SystemSuperApp systemSuperApp;
	  @Resource(name="defaultAppId")
		String defaultAppId;
	  @Resource(name="menuTodo")
		String menuTodo;
	  
	  @Autowired
	  SendMessageService sendMessageService;
		
	@Autowired
	ExtTaskTodoRepository extTaskTodoRepository;
	@Scheduled(cron = "0 0 10 * * ?")  
    public void remind() {  
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime start_ = LocalDate.now().minusDays(1).atTime(0, 0, 0);
		LocalDateTime end_ = LocalDate.now().minusDays(1).atTime(23, 59, 59);
		Date start=Date.from(start_.atZone(zone).toInstant());
		Date end=Date.from(end_.atZone(zone).toInstant());
		List<CountTodo> countTodos=extTaskTodoRepository.countTodo(start,end);
		for(CountTodo count:countTodos){
			StatusMessage statusMessage =tixing(count.getFromUser(),count.getCount(),menuTodo);
			sendMessageService.sendMessage(sendMessageService.sendUrl(gocomeApiUrl, defaultAppId, method_single), statusMessage);
		}
	}
	private StatusMessage tixing(String touser,long count,String url){
		StatusMessage statusMessage = new StatusMessage();
		statusMessage.setTouser(touser);
		StatusMessage.Begin begin=new StatusMessage.Begin();
		StatusMessage.Content contentext=new StatusMessage.Content();
		List<StatusMessage.Form> forms=new ArrayList<>();
		contentext.setTitle("待办消息提醒");
		contentext.setDetail("老板我刚帮您追踪到了，昨日有"+count+"条待办。");
		begin.setContent(contentext);
		//begin.setUrl(url);
		//begin.setIos(url);
		//begin.setAndroid(url);
		statusMessage.setBegin(begin);
		return statusMessage;
	}
}